/*
Program Name
Copyright (C) 2008 Matthew Perry

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using SourceTools.IO;

namespace SourceTools.UI
{
	/// <summary>
	/// Description of UIGamePath.
	/// </summary>
	public partial class UIGamePath : Form
	{
		/// <summary>
		/// Creates a UI game path
		/// </summary>
		public UIGamePath()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		string ReadDict(Dictionary<string, string> dict, string key)
		{
			if (dict.ContainsKey(key))
				return dict[key];
			else
				return "";
		}
		void WriteDict(Dictionary<string, string> dict, string key, string value)
		{
			if (dict.ContainsKey(key))
				dict[key] = value;
			else
				dict.Add(key, value);
		}
		
		/// <summary>
		/// Save the fields
		/// </summary>
		public void SaveFields()
		{
			Dictionary<string, string> dict = SourceSettings.GamePaths;
			
			WriteDict(dict, SourceSettings.GAMEPATH_CSTRIKE, txtCStrike.Text);
			WriteDict(dict, SourceSettings.GAMEPATH_GMOD, txtGMod.Text);
			WriteDict(dict, SourceSettings.GAMEPATH_HL2, txtHL2.Text);
			WriteDict(dict, SourceSettings.GAMEPATH_HL2EP1, txtHL2EP1.Text);
			WriteDict(dict, SourceSettings.GAMEPATH_HL2EP2, txtHL2EP2.Text);
			WriteDict(dict, SourceSettings.GAMEPATH_DOD, txtDOD.Text);
			WriteDict(dict, SourceSettings.GAMEPATH_TF2, txtTF2.Text);
			
			SourceSettings.GamePaths = dict;
		}
		
		/// <summary>
		/// Load the fields
		/// </summary>
		public void FillFields()
		{
			Dictionary<string, string> dict = SourceSettings.GamePaths;
			txtCStrike.Text = ReadDict(dict, SourceSettings.GAMEPATH_CSTRIKE);
			txtGMod.Text = ReadDict(dict, SourceSettings.GAMEPATH_GMOD);
			txtHL2.Text = ReadDict(dict, SourceSettings.GAMEPATH_HL2);
			txtHL2EP1.Text = ReadDict(dict, SourceSettings.GAMEPATH_HL2EP1);
			txtHL2EP2.Text = ReadDict(dict, SourceSettings.GAMEPATH_HL2EP2);
			txtDOD.Text = ReadDict(dict, SourceSettings.GAMEPATH_DOD);
			txtTF2.Text = ReadDict(dict, SourceSettings.GAMEPATH_TF2);
		}
		
		void UIGamePathLoad(object sender, EventArgs e)
		{
			
		}
		
		void BtnOKClick(object sender, EventArgs e)
		{
			SaveFields();
			this.Close();
		}
		
		string BrowseFor(string desc, string def)
		{
			fbrowse.Description = desc;
			DialogResult dr = fbrowse.ShowDialog();
			if (dr != DialogResult.Cancel)
			{
				return fbrowse.SelectedPath;
			}
			else
			{
				return def;
			}
		}
		
		void BtnCStrikeClick(object sender, EventArgs e)
		{
			txtCStrike.Text = BrowseFor("Select the cstrike folder from Counter-Strike: Source", txtCStrike.Text);
		}
		
		void BtnGModClick(object sender, EventArgs e)
		{
			txtGMod.Text = BrowseFor("Select the garrysmod folder from Garry's Mod", txtGMod.Text);
		}
		
		void BtnHL2Click(object sender, EventArgs e)
		{
			txtHL2.Text = BrowseFor("Select the hl2 folder from Half-Life 2", txtHL2.Text);
		}
		
		void BtnHL2EP1Click(object sender, EventArgs e)
		{
			txtHL2EP1.Text = BrowseFor("Select the episodic folder from Half-Life 2: Episode One", txtHL2EP1.Text);
		}
		
		void BtnHL2EP2Click(object sender, EventArgs e)
		{
			txtHL2EP2.Text = BrowseFor("Select the ep2 folder from Half-Life 2: Episode Two", txtHL2EP2.Text);
		}
		
		void BtnDODClick(object sender, EventArgs e)
		{
			txtDOD.Text = BrowseFor("Select the dod folder from Day of Defeat: Source", txtDOD.Text);
		}
		
		void BtnTF2Click(object sender, EventArgs e)
		{
			txtTF2.Text = BrowseFor("Select the tf folder from Team Fortress 2", txtTF2.Text);
		}
		
		void UIGamePathShown(object sender, EventArgs e)
		{
		}
		
		void Label7Click(object sender, EventArgs e)
		{
			
		}
	}
}
